<template>
  <div class="patientManage">
    <div class="topSearch">
      <div class="s-left">
        <el-input class="s-ipt" placeholder="请输入患者名称" prefix-icon="el-icon-search" v-model.trim="tableData.listInfo1.form.name" @change="searchTab"></el-input>
        <!-- <el-button type="text">更多搜索<i class="el-icon-arrow-down el-icon--right"></i></el-button> -->
      </div>
      <div class="s-btns"  v-if="getUserType!=1">
         <el-button type="primary" @click="pop.pop1.title = '添加患者';pop.pop1.flag = true">新增</el-button>
      </div>
    </div>
    <!-- <div class="highSearch">
      <el-row :gutter="20">
        <el-col :span="8">fasdsdafd--</el-col>
      </el-row>
    </div> -->
    <div class="tableMain" v-if="tableData.listInfo1.list.length">
      <!-- <el-table :data="tableData.listInfo1.list" stripe fixed :height="tableData.listInfo1.height" style="width: 100%"> -->
      <el-table :data="tableData.listInfo1.list" ref="eltable" stripe fixed height="100%" style="width: 100%">
        <el-table-column fixed="left" label="序号" width="80">
            <template slot-scope="scope">
              {{ (tableData.listInfo1.form.pageNum - 1) * tableData.listInfo1.form.pageSize + scope.$index + 1 }}
            </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip :prop="KEYS.key1" label="姓名"></el-table-column>
        <el-table-column show-overflow-tooltip :prop="KEYS.key10" label="医院科室">
          <template slot-scope="scope">
            {{scope.row.hospital}} -- {{scope.row.department}}
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip :prop="KEYS.key2" label="性别" width="100"></el-table-column> <!-- width="60" -->
        <el-table-column show-overflow-tooltip :prop="KEYS.key3" label="年龄" width="100"></el-table-column> <!-- width="80" -->

        <!-- <el-table-column show-overflow-tooltip :prop="KEYS.key6" label="创建人" width="100"></el-table-column> -->
        <el-table-column show-overflow-tooltip :prop="KEYS.key7" label="创建时间" width="180"></el-table-column>
        <!-- <el-table-column show-overflow-tooltip :prop="KEYS.key8" label="最后修改人" width="100"></el-table-column> -->
        <el-table-column show-overflow-tooltip :prop="KEYS.key9" label="最后修改时间" width="180"></el-table-column>
        <el-table-column fixed="right" label="操作" width="150">
          <template slot-scope="scope">
            <el-tooltip effect="light" content="编辑影像" placement="left">
              <i class="handleBtn amsp el-amsp-yixueyingxiang1" @click="$router.push({name: 'medicalRecordManage', query: {id: scope.row.id}})"></i>
            </el-tooltip>
            <el-tooltip effect="light" content="编辑" placement="left" v-if="getUserType!=1">
              <i class="handleBtn amsp el-amsp-yonghubianji1" style="font-size: 18px;" @click="editFormPatient(scope.row)"></i>
            </el-tooltip>
            <el-tooltip effect="light" content="删除" placement="left" v-if="getUserType!=1">
              <i class="handleBtn el-icon-delete" style="color: red" @click="deletFormPatient(scope.row)"></i>
              </el-tooltip>
          </template>
        </el-table-column>
    </el-table>
    </div>
    <amsp-page 
      tableName="listInfo1"
      :pageSize="tableData.listInfo1.form.pageSize"
      @changeSize="changeSize($event, 'listInfo1')"
      @setPage="changePage($event, 'listInfo1')"
      :tableForm="tableData.listInfo1.form"
      @toFirst="changePage(1, 'listInfo1')"
      @toEnd="changePage(tableData.listInfo1.form.pages, 'listInfo1')"/>
  <amsp-pop class="addEditPatient" ref="addEditPatient"  :title="pop.pop1.title" width="560px":visible.sync="pop.pop1.flag" :before-close="closeAddEdit">
      <div class="addEditPage">
      <el-form class="addEditFormPatient" ref="addEditFormPatient" :model="pop.pop1.form" :rules="RULES"  label-position="left" label-width="80px" @submit.native.prevent>
          <el-form-item label="姓名" :prop="KEYS.key1">
            <el-input placeholder="请输入患者姓名" v-model.trim="pop.pop1.form[KEYS.key1]"></el-input>
          </el-form-item>
          <el-form-item label="性别" :prop="KEYS.key2">
            <el-radio-group v-model.trim="pop.pop1.form[KEYS.key2]">
              <el-radio label="男"></el-radio>
              <el-radio label="女"></el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="所属科室" :prop="KEYS.key10" v-if="getUserType!=1">
            <el-select v-model="pop.pop1.form[KEYS.key10]" clearable filterable placeholder="请选择所属科室" style="width: 100%">
              <el-option
                  v-for="item in arr_2"
                  :key="item.id"
                  :label="item.hospital+'--'+item.department"
                  :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="年龄(岁)" :prop="KEYS.key3">
            <el-input placeholder="请输入患者年龄" type="number"  v-model.trim="pop.pop1.form[KEYS.key3]"></el-input>
          </el-form-item>
          <el-form-item label="身份证号" :prop="KEYS.key4">
            <el-input placeholder="请输入身份证号" v-model.trim="pop.pop1.form[KEYS.key4]"></el-input>
          </el-form-item>
          <el-form-item label="联系电话" :prop="KEYS.key5">
            <el-input placeholder="请输入联系电话" v-model.trim="pop.pop1.form[KEYS.key5]"></el-input>
          </el-form-item>
      </el-form>
      <div class="baseFormBtns" id="baseFormBtns">
        <el-button round @click="closeAddEdit">取消</el-button>
        <el-button round type="primary" @click="addPatient">确定</el-button>
      </div>
    </div>
  </amsp-pop>
  </div>
</template>

<script>
import {addPatient, editPatient, queryPatientPage} from '@/depends/api/patient'
import {queryDepartmentPage} from '@/depends/api/department'
import {deepCopy} from '@/utils/tools/tools'
import {KEYS_RULES_MIXIN, KEYS} from './component/keys.js'
import {mapGetters} from 'vuex'

export default {
  components: {
  },
  mixins: [KEYS_RULES_MIXIN],
  // provide () {
  //   return {
  //     vMain: this
  //   }
  // },
  data() {
    return {
      pop: {
        pop1: {
          flag: false,
          title: '添加患者',
          form: {
            [KEYS.key0]: '',
            [KEYS.key1]: '',
            [KEYS.key2]: '男',
            [KEYS.key3]: '',
            [KEYS.key4]: '',
            [KEYS.key5]: '',
            [KEYS.key10]: '',
          }
        }
      },
      arr_2: [],
      tableData: {
        listInfo1: {
          loading: false,
          form: {
            name: '',

            pageNum: 1,
            pageSize: 15,
            total: 30,
            pages: 1,
          },
          list: []
        }
      }
    };
  },
  computed:{
    ...mapGetters('user', ['getUserType']),
  },
  created() {
    this.queryPatientPage()
    this.queryDepartmentPage()
  },
  mounted() {
    
  },
  methods:{
    queryDepartmentPage () {
      let params = {pageNum:1,pageSize:1000};
      queryDepartmentPage(params).then(res => {
        if (res.data.status !== 200) { return }
        this.arr_2 = res.data.data.list;
      })
    },
    deletFormPatient (a) {
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(() => {
          const params = { id: a.id, isDeleted: 1 } 
          editPatient(params).then(res => {
            if (res.data.status !== 200) { return }
            this.$message.success('删除成功!')
            this.queryPatientPage()
          })
        }).catch(() => {
          // this.$message({
          //   type: 'info',
          //   message: '已取消删除'
          // });          
        });
    },
    searchTab () {
      this.changePage(1, 'listInfo1')
    },
    changeSize (a,b) {
      this.tableData[b].form.pageSize = a
      this.changePage(1, b)
    },
    changePage (a,b) {
      this.tableData[b].form.pageNum = a
      this.queryPatientPage()
    },
    queryPatientPage () {
      let params = deepCopy(this.tableData.listInfo1.form)
      this.tableData.listInfo1.loading = true
      queryPatientPage(params).then(res => {
        this.tableData.listInfo1.loading = false
        if (res.data.status !== 200) { return }
        this.tableData.listInfo1.list = res.data.data.list
        this.tableData.listInfo1.form.pageNum = res.data.data.pageNum
        this.tableData.listInfo1.form.total = res.data.data.total
        this.tableData.listInfo1.form.pages = res.data.data.pages
        this.$nextTick(() => {
          this.$refs.eltable.bodyWrapper.scrollTop = 0
        })
      })
    },
    editFormPatient (a) {
      this.pop.pop1.flag = true
      this.pop.pop1.title = '编辑患者'
      this.$nextTick(() => {
        for (let i in this.pop.pop1.form) {
          this.pop.pop1.form[i] = a[i]
        }
      })
    },
    isPass(form) {
      return new Promise((reslove, reject) => {
        this.$refs[form].validate((valid) => {
          reslove(valid);
        });
      });
    },
    closeAddEdit () {
      this.$refs.addEditFormPatient.resetFields();
      this.pop.pop1.form[KEYS.key0] = ''
      this.pop.pop1.flag = false
    },
    async addPatient () {
      let isPass = await this.isPass('addEditFormPatient');
      if (!isPass) {return}
      let params = deepCopy(this.pop.pop1.form)
      if (params.id) {
        editPatient(params).then(res => {
          if (res.data.status !== 200) { return }
          this.$message.success('编辑成功!')
          this.closeAddEdit()
          this.queryPatientPage()
        })
      } else {
        addPatient(params).then(res => {
          if (res.data.status !== 200) { return }
          this.$message.success('新增成功!')
          this.closeAddEdit()
          this.queryPatientPage()
        })
      }
    },
    goForm () {

    }
  },
  beforeDestroy() {
  },
}
</script>
<style scoped lang="less">
.patientManage {
  height: 100%;
  padding: 20px;
  display: flex;
  flex-direction: column;
  padding-bottom: 65px;
  // overflow-y: hidden;
  .tableMain {
    flex: 1;
  }
  .addEditPatient {
    #baseFormBtns.baseFormBtns {
      position: initial;
    }
  }
}
</style>
